package com.zuipin.controller;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zuipin.service.IBalanceRecordService;
import com.zuipin.service.IDrawNumService;
import com.zuipin.service.IIntegralRecordService;
import com.zuipin.service.ILoginRuleNumService;
import com.zuipin.service.IMemberLevelService;
import com.zuipin.service.IRedPacketService;
import com.zuipin.service.IRuleSetService;

import io.swagger.annotations.Api;

/**
 * 
 * @ClassName: RuleSetController 
 * @author wuyicheng
 * @time 2017年6月13日上午10:21:58
 */
@Api(value = "规则设置-xujinding", description = "规则设置-xujinding")
@RequestMapping(value = "/ruleSet")
@Controller
public class UserBehaviorController {
	protected final Logger			log	= Logger.getLogger(getClass());
	
	private IRuleSetService			ruleSetService;
	@Resource
	private IIntegralRecordService	integralRecordService;
	@Resource
	private IDrawNumService			drawNumService;
	@Resource
	private IBalanceRecordService	balanceRecordService;
	
	@Resource
	private IRedPacketService		redPacketService;
	
	@Resource
	private IMemberLevelService		memberLevelService;
	
	@Resource
	private ILoginRuleNumService	loginRuleNumService;
	
	/**
	 * 派发用户行为
	 * @param sysId
	 * @param sysUserId
	 * @param memberId
	 * @param device
	 * @param activityType
	 * @throws CustomException
	 * @throws ParseException
	 */
	/*	public void sendByUserBehavior(Long sysId, Long sysUserId, Long memberId, String device, String activityType) throws CustomException, ParseException {
			RuleSet param = new RuleSet();
			param.setDelFlag(ConstantsUtil.DEL_FLAG_NO_0);
			param.setRuleType(RuleType.USER_BEHAVIOR.toString());
			param.setStatus(Status.ALR_ENABLE.toString());
			String currTime = DateUtils.getCurrentDateTime();
			param.setStartTime(currTime);
			param.setEndTime(currTime);
			param.setSysId(sysId);
			param.setDevice(device);
			param.setActivityType(activityType);
			List<RuleSet> list = ruleSetService.findRule(param, "enableTime", "DESC");
			
			if (list == null || list.size() < 1) {
				log.info("查无相应的用户行为规则，无法派送");
				return;
			}
			MemberLevel memberLevel = null;
			if (ShowType.LOGIN.toString().equals(activityType)) {
				memberLevel = memberLevelService.findByMemberIdAndSysId(memberId, sysId);
				if (memberLevel == null) {
					throw new CustomException(ErrorCode.RULE_OUT_MEMBER_LEVEL.getCode(), "会员等级不符合要求");
				}
			}
			
			for (RuleSet rs : list) {
				if (ShowType.LOGIN.toString().equals(activityType)) {
					boolean isLevelSuccess = ruleSetService.checkLimitMemberLevel(memberLevel, rs);
					if (isLevelSuccess) {
						log.info("会员行为：规则限制等级-" + rs.getLimitMemberLevel() + "，memberId为" + memberId + "等级为" + memberLevel.getId());
						continue;
					}
					LoginRuleNum findNum = new LoginRuleNum();
					findNum.setRuleId(rs.getId());
					findNum.setMemberId(memberId);
					findNum.setDelFlag(ConstantsUtil.DEL_FLAG_NO_0);
					findNum.setSysId(sysId);
					List<LoginRuleNum> loginList = loginRuleNumService.findListBySelect(findNum);
					if (loginList == null || loginList.size() < 1) {
						loginRuleNumService.findListBySelect(findNum);
					} else {
						log.info("会员行为：首次登录-" + "，memberId为" + memberId + "对应的规则Id" + rs.getId() + "登录了" + loginList.size() + "次");
						continue;
					}
				}
				
				// 送积分
				if (ShowType.GIVE_INTEGRAL.toString().equals(rs.getShowType())) {
					log.info("用户行为--送积分memberId=" + memberId + "数量=" + rs.getGiveNum());
					if (ShowType.REG.toString().equals(activityType)) {
						integralRecordService.addIntegralRecord(memberId, sysId, null, null, rs.getGiveNum().intValue(), IntegralType.REGISTER, "用户行为送积分");
					} else {
						integralRecordService.addIntegralRecord(memberId, sysId, null, null, rs.getGiveNum().intValue(), IntegralType.LOGIN_GIVE, "用户行为送积分");
					}
				}
				// 送抽奖次数
				if (ShowType.GIVE_DRAW_NUM.toString().equals(rs.getShowType())) {
					log.info("用户行为--抽奖次数memberId=" + memberId + "数量=" + rs.getGiveNum());
					if (StringUtil.isBlank(rs.getGiveCode())) {
						String[] no = rs.getGiveCode().split(",");
						for (int i = 0; i < no.length; i++) {
							drawNumService.modityDrawNum(memberId, Long.valueOf(no[i]), sysId, rs.getGiveNum().intValue(), null);
						}
					}
				}
				// 送充值
				if (ShowType.GIVE_TOPUP_BALANCE.toString().equals(rs.getShowType())) {
					log.info("用户行为--送充值memberId=" + memberId + "数量=" + rs.getGiveNum());
					if (ShowType.REG.toString().equals(activityType)) {
						balanceRecordService.updateBalance(memberId, sysId, null, null, rs.getGiveNum(), BalanceType.REG_GIVE, "用户行为送充值");
					} else {
						balanceRecordService.updateBalance(memberId, sysId, null, null, rs.getGiveNum(), BalanceType.LOGIN_GIVE, "用户行为送充值");
					}
				}
				// 送红包
				if (ShowType.GIVE_RED_PACKET.toString().equals(rs.getShowType())) {
					log.info("用户行为--送红包memberId=" + memberId + "数量=" + rs.getGiveNum());
					String[] no = rs.getGiveCode().split(",");
					List<Long> ruleIds = new ArrayList<>();
					for (int i = 0; i < no.length; i++) {
						ruleIds.add(Long.valueOf(no[i]));
					}
					redPacketService.sendRedPacket(ruleIds, sysId, rs.getGiveNum().intValue(), memberId, SysUser.SYS_USER, null,
							RuleType.USER_BEHAVIOR.toString() + "_" + PrizeType.GIVE_RED_PACKET.toString());
				}
				// 送商品
				if (ShowType.GIVE_PRO.toString().equals(rs.getShowType())) {
					log.info("用户行为--送商品memberId=" + memberId + "数量=" + rs.getGiveNum());
				}
			}
		}*/
}
